package com.liyuxiang.yygh.sms.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.liyuxiang.yygh.sms.service.SmsService;
import com.liyuxiang.yygh.sms.utils.ConstantPropertiesUtils;
import com.liyuxiang.yygh.sms.utils.HttpUtils;
import com.liyuxiang.yygh.vo.msm.MsmVo;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Service
public class SmsServiceImpl implements SmsService {

    @Override
    public boolean send(String phone, String code) {
        if (StringUtils.isEmpty(phone)) {
            return false;
        }
        String host = "https://dfsns.market.alicloudapi.com";
        String path = "/data/send_sms";
        String method = "POST";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + ConstantPropertiesUtils.APP_CODE);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("content", "code:" + code);
        bodys.put("phone_number", phone);
        bodys.put("template_id", "TPL_09887");
        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            JSONObject jsonObject = (JSONObject) JSONObject.parse(EntityUtils.toString(response.getEntity()));
            String resultCode = (String) jsonObject.get("status");
            if (resultCode.equals("OK")) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * mq发送短信
     *
     * @param msmVo
     * @return
     */
    @Override
    public boolean send(MsmVo msmVo) {
        if (!StringUtils.isEmpty(msmVo.getPhone())) {
            boolean isSend = this.send(msmVo.getPhone(), msmVo.getParam(), msmVo.getTemplateCode());
            return isSend;
        }
        return false;
    }

    public boolean send(String phone, Map<String, Object> param, String templateCode) {
        if (StringUtils.isEmpty(phone)) {
            return false;
        }
        String host = "https://dfsns.market.alicloudapi.com";
        String path = "/data/send_sms";
        String method = "POST";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + ConstantPropertiesUtils.APP_CODE);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = null;
        Map<String, String> bodys = null;
        switch (templateCode) {
            //订单预约提醒模版
            case "TPL_09891":
                querys = new HashMap<>();
                bodys = new HashMap<>();
                bodys.put("content", "patient_name:" + param.get("patient_name").toString() + ",hosname:" + param.get("hosname").toString() + ",depname:" + param.get("depname").toString() + ",fetch_address:" + param.get("fetch_address").toString());
                bodys.put("phone_number", phone);
                bodys.put("template_id", templateCode);
                break;
            //每天就医提醒模版
            case "TPL_09897":
                querys = new HashMap<>();
                bodys = new HashMap<>();
                bodys.put("content", "patient_name:" + param.get("patient_name").toString() + ",title:" + param.get("title").toString() + "，,reserveDate:" + param.get("reserveDate").toString());
                bodys.put("phone_number", phone);
                bodys.put("template_id", templateCode);
                break;
        }

        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            JSONObject jsonObject = (JSONObject) JSONObject.parse(EntityUtils.toString(response.getEntity()));
            String resultCode = (String) jsonObject.get("status");
            if (resultCode.equals("OK")) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
